package com.danco.training.zaneuskaya.repository.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.danco.training.zaneuskaya.domain.City;
import com.danco.training.zaneuskaya.domain.Country;
import com.danco.training.zaneuskaya.domain.Education;
import com.danco.training.zaneuskaya.domain.User;
import com.danco.training.zaneuskaya.repository.EducationDao;
/**
 * @author Zaneuskaya Aliaksandra
 *
 */
@Repository
public class EducationDaoImpl extends AbstractHibernateDao<Education, Integer> implements EducationDao{

	private static final String EDUCATION_END = "educationEnd";
	private static final String USER_EDUCATION = "userEducation";
	private static final String EDUCATION_TYPE = "educationType";
	private static final String EDUCATION_CITY = "educationCity";
	private static final String EDUCATION_START = "educationStart";
	private static final String EDUCATION_COUNTRY = "educationCountry";

	@Override
	public Education read(Integer PK) throws HibernateException {
		return super.read(Education.class, PK);
	}

	@Override
	public List<Education> readAll() throws HibernateException {
		return super.readAll(Education.class,EDUCATION_START);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Education> findEducationsByCity(City city) throws HibernateException{
		List<Education> entities = new ArrayList<Education>();
		Criteria criteria = getSession().createCriteria(City.class);
		criteria.add(Restrictions.eq(EDUCATION_CITY, city));
		
		entities =  modifyCriteria(criteria,EDUCATION_START).list();
		return entities;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Education> findEducationByUser(User user) throws HibernateException{
		List<Education> entities = new ArrayList<Education>();
		Criteria criteria = getSession().createCriteria(Education.class);
		criteria.add(Restrictions.eq(USER_EDUCATION, user));
		
		entities = modifyCriteria(criteria,EDUCATION_START).list();
		return entities;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Education> findEducationsByCountry(Country country) throws HibernateException{
		Criteria criteria = getSession().createCriteria(Country.class);
		criteria.add(Restrictions.eq(EDUCATION_COUNTRY, country));
		
		return modifyCriteria(criteria,EDUCATION_START).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Education> findEducationsByPeriod(Date from, Date to) throws HibernateException{
		Criteria criteria = getSession().createCriteria(Education.class);
		criteria.add(Restrictions.between(EDUCATION_START, from, to));
		criteria.add(Restrictions.between(EDUCATION_END, from, to));
		return modifyCriteria(criteria,EDUCATION_START).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Education> findUserEducationsByType(String educationType, User user) throws HibernateException{
		Criteria criteria = getSession().createCriteria(Education.class);
		criteria.add(Restrictions.eq(EDUCATION_TYPE,educationType));
		criteria.add(Restrictions.eq(USER_EDUCATION, user));
	
		return  modifyCriteria(criteria,EDUCATION_START).list();
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<Education> findEducationsByLocation(Country country, City city) throws HibernateException{
	
		Criteria criteria = getSession().createCriteria(Country.class);
		criteria.add(Restrictions.eq(EDUCATION_COUNTRY, country));
		criteria.add(Restrictions.eq(EDUCATION_CITY, city));
		
		return  modifyCriteria(criteria,EDUCATION_START).list();
	}

	

}
